*This file uses 1950 census data and aggregated reparations data to create a local shift-share instrument

////////////////////////////////////////////////

*Set directory
cd "W:\Matti M"


*-- Use the ready-made 1950 census data
use "W:\hankkeesta_a82\2020-01-17\vl50_laaja.dta" , clear


*---  Renaming variables
destring jnro, gen(id)

rename AKUNU39 mun39
label var mun39 "Municipality of residence, 1939"

rename TOLA39 ind39
label var ind39 "Industry in 1939"
destring ind39, replace

destring PIKA50, gen(age50)
drop PIKA50

gen age39=age50-11


*Calculate municipal population in the sample

gen individual=1

*Keep working age to do the shares
keep if age39>15&age39<65
tab ind39 

collapse (sum) individual, by(ind39 mun39)

*Generate share of industries in municipalities (strings)

preserve
collapse (sum) individual, by(mun39 ind39)
bys mun39: egen all=sum(individual)
gen share_ind=individual/all
drop individual all
save "$temp\Long_shares2", replace
restore

*Omit those without known industry
drop if  ind39==99|ind39==91


*Generate reparations per wokers by industry
preserve
collapse (sum) individual, by(ind39)
tostring ind39, replace
*Import reparations allocated to 2 digit level
joinby ind39 using "$path\repadata.dta", unmatched(master)
drop _merge
replace reparations=0 if reparations==.
gen repa_scaled=reparations/(individual)
tempfile repadata
save `repadata'
save "$temp\repadata_scaled", replace
restore


*Generate share of industries in municipalities and the shift share

preserve
collapse (sum) individual, by(mun39 ind39)
bys mun39: egen all=sum(individual)
gen share_ind=individual/all
drop individual all
destring mun39 , replace
save "$temp\Long_shares", replace
restore


preserve
collapse (sum) individual, by(mun39 ind39)
bys mun39: egen all=sum(individual)
gen share_ind=individual/all
tostring ind39, replace
joinby ind39 using `repadata'


gen bartik1=repa_scaled*share_ind


*Create groups
gen timber_paper=(ind39=="25"|ind39=="27")
gen hs=(ind39=="31"|ind39=="32"|ind39=="27"|ind39=="36"|ind39=="37"|ind39=="38"|ind39=="39")
gen ls=(ind39=="19"|ind39=="33"|ind39=="34"|ind39=="35"|ind39=="25"|ind39=="4")


gen bartik_notimber=repa_scaled*share_ind*(timber_paper==0)

gen bartik_hs=repa_scaled*share_ind*(hs==1)
gen bartik_ls=repa_scaled*share_ind*(ls==1)

keep mun39 ind39 bartik1 share_ind bartik_notimber  repa_scaled bartik_hs bartik_ls 

reshape wide bartik1 share_ind bartik_notimber  repa_scaled bartik_hs bartik_ls ,i(mun39) j(ind39) string


*Generate shift share exposure variables

egen bartik=rowtotal(bartik1*)

egen bartikhs=rowtotal(bartik_hs*)

egen bartikls=rowtotal(bartik_ls*)

egen bartiknotimber=rowtotal(bartik_notimber*)

keep  mun39 bartik bartiknotimber bartikls bartikhs  share_ind*  repa_scaled*

order share_ind* 

foreach var of varlist share_ind1-share_ind89{
replace `var'=0 if `var'==.
}

order repa_scaled* 
foreach var of varlist repa_scaled1-repa_scaled89{
replace `var'=0 if `var'==.
}

foreach var of varlist repa_scaled1-repa_scaled89{
sum `var', meanonly
replace `var'=r(max) if `var'==0
}

tempfile bartik
save `bartik'
restore


*Population per municipality
collapse (sum) individual, by(mun39)

joinby mun39 using `bartik', unmatched(master)


*Standardize
egen bartik_standardized=std(bartik) 
xtile  bartik_group4=bartik , nq(4)


*Omit muncipalities with fewer than 5 values in the sample due to Stat Finland rules. 
drop if individual<5



***********************************************
saveold "municipality_shares_bartik_1939_all_munis", replace

saveold "$temp\municipality_shares_bartik_1939_all_munis", replace
***********************************************




***********************************************
*Link with control variables
***********************************************

use "$temp\municipality_shares_bartik_1939_all_munis", clear

*Municipal level controls from Manufactring surveys, Sarvimäki 2011 and Meriläinen et al. 2023. See paper for details.
drop _merge
joinby mun39 using "$path\municipality_controls_full.dta" 

*Omit some missing variables
drop if  laani==.

egen bartik_standardized2=std(bartik)

save "$temp\municipality_shares_bartik_1939", replace

